package com.gtd.ws

import grails.converters.*

/**
 * Utility for rest service controller
 * 
 * Display all domain classes: /rest/define/all
 * Display properties of a domain class: /rest/define/$className
 * 
 * @author  hoatle
 *          hoatlevan at gmail dot com
 * @since   1.0
 * @created Jul 5, 2009
 */
class DefineObjectController {
	def res
	
	//BlackListed Domain Class
	def blackListed = [
	                   	
	                   ]
	
	
    def define = {
    	if (params.id.equals("all")) {
    		def dList = new ArrayList()
    		grailsApplication.domainClasses.each({
    			//TODO: Add more properties
    			def clazz = [
    			             "class": it.clazz,
    			             name: it.name
    			             ]
    			//TODO: If blacklisted, exclude it
    			dList.add(clazz)
    		})
    		
    		
    		res = [
    		       domainList: dList
    		       ]
    	} else {
    		def cName = getDomainClass(params.id)
    		
    		if (cName) {
    			//TODO: get all neccessary properties
    		} else {
    			//Erro: Not found
    		}
    	}
    	
    	Renderer.render(this, res)
    }
	
	
	
	private void getDomainClass(name) {
		
		grailsApplication.domainClasses.each({
			//Check if blacklisted
			def cName = it.name
			if (cName.compareToIgnoreCase(name)) {
				return cName
			}
			return null
		})
	}
}
